class ToolsController < ApplicationController
  before_filter :only_admin_can_crud, :only => [ :new, :create, :edit, :update]  
  
  layout 'base.html', :except => :seo
  
  def index
    @tools = params[:tool_type_id].blank? ? Tool.all : Tool.find_all_by_tool_type_id(params[:tool_type_id])
    @tool_type = params[:tool_type_id].blank? ? nil : ToolType.find(params[:tool_type_id])
    @tool_types = ToolType.all
    @seo_title = "麦吐司网-有趣、实用、免费的吐司大全"
    @seo_keywords = '免费,有趣,实用,吐司,工具,大全'
    @seo_description = "所有的麦吐司网-有趣、实用、免费的工具都在此"
  end
  
  def new
    @tool = Tool.new
  end
  
  def create
    @tool = Tool.new(params[:tool])
    if @tool.save      
      redirect_to tool_url(@tool)
    else
      render :new
    end
  end
  
  def edit
    @tool = Tool.find(params[:id])
  end
  
  def update
    @tool = Tool.find(params[:id])
    if @tool.update_attributes(params[:tool])
      redirect_to tool_url(@tool)
    else
      render :edit
    end
  end
  
  def show
    @tool = Tool.find(params[:id])
    @tool.be_cared!
    @seo_title = "麦吐司网-#{@tool.tool_type.name}-#{@tool.name}-#{@tool.synopsis}-#{@tool.key_words.split(' ').join(',')}"
    @seo_keywords = (@tool.key_words.split(' ') + [@tool.tool_type.name]).join(',')
    @seo_description = @tool.synopsis
  end
  
  def send_email
    
  end
  
  def seo    
    @tools = Tool.all
  end
  
  private 
  def only_admin_can_crud
    redirect_to tools_url if not current_user.admin?      
  end
  
  def authorize
  end
end
